\section{Simulating Pois(1) random variables}

In this problem $X$ is a discrete random variable taking values in $\mathbb{Z}_+=\{1,2,\ldots\}$ with density function $f(\cdot)$ and $\xi$ is Unif(0, 1).\\

\noindent
In the following we consider $\eta = \sum_{k=1}^\infty\eta_k$ where $\eta_k = \lfloor\xi+1-\sum_{j=1}^{k-1}f(j)\rfloor$.

\subsection{What are the possible values of $\eta_k$}
Since $f(\cdot)$ is the density function for a random variable in $\mathbb{Z}_+$ the function $\sum_{j=1}^{k-1}f(j)$ must take values on the interval $[0;1]$.\\

\noindent
Using this and the fact that $\xi$ is a distribution on $[0;1]$, we see that $\xi+1-\sum_{j=1}^{k-1}f(j)\in\brackk{0;2}$, where the endpoint 2 corresponds to $\xi=1$ and $\sum_{j=1}^{k-1}f(j)=0$. Since $\xi\sim\textrm{Unif(0,1)}$ we have $P\paren{\xi=1}=0$. Thus $P\paren{\xi+1-\sum_{j=1}^{k-1}f(j)=2} = 0$.\\

\noindent
This means that $\eta_k \in \{0,1,2\}$ but with $P(\eta_k = 2)=0$.

\subsection{Prove $X = \eta$ in distribution}
To prove $X \overset{d}{=} \eta$ we need to establish that $P(X\le x) = P(\eta \le x)$ for all $x\in\mathbb{R}$.\\

\noindent
Note that the distribution function $F(x)$ for $X$ is given as 
\begin{eqnarray*}
F(x)&=&P(X\le x)\\
    &=& \sum_{i =1}^{\lfloor x \rfloor} f(i)\\
    &=& P(X\le \lfloor x\rfloor)
\end{eqnarray*}
and that
\begin{eqnarray*}
P(\eta \le x) &=& P(\sum_{k=1}^\infty\lfloor\xi+1-F(k-1)\rfloor = x)\\
              &=& P(\sum_{k=1}^\infty\lfloor\xi+1-F(k-1)\rfloor = \lfloor x\rfloor) \\
              &=& P(\eta \le \lfloor x \rfloor)
\end{eqnarray*}

\noindent
furthermore since $P(X\le 0) = P(\eta\le 0) =0$ we only need to establish that $P(X\le x) = P(\eta \le x)$ for all $x \in \mathbb{Z}_+$.\\

\noindent
Since $F(\cdot)$ is a distribution function it is increasing. Thus $\eta_k = \lfloor\xi+1-F(k-1)\rfloor$ is a decreasing funktion in $k$, which implies that $\eta=x$ is equivalent to

\begin{equation*}
\eta_k = \left\{
\begin{array}{rl}
1 & \text{if } k \in \{1, \ldots, x\},\\
0 & \text{otherwise.}
\end{array} \right.
\end{equation*}
This also means that $P(\eta \le x) = P(\sum_{k=1}^\infty\eta_k \le x) = P(\sum_{k=x+1}^\infty\eta_k = 0) = P(\eta_{x+1} = 0)$. We can now use this to prove what we set out to prove
\begin{eqnarray*}
P(\eta \le x) &=& P(\eta_{x+1} = 0)\\
 &=& P(\lfloor\xi+1-F(x)\rfloor = 0)\\
 &=& P(\xi+1-F(x) < 1)\\
 &=& P(\xi < F(x))\\
 &=& P(\xi \le F(x))\\
 &=& P(F^{-1}(\xi) \le x) \\
 &=& P(X \le x)
\end{eqnarray*}
where we have utilized the knowledge we gained from the inverse transform method, that if $X$ is a random variable with distribution function $F$ and $\tilde{X} = F^{-1}(U)$ where $U$ $\sim$ Unif(0,1) then $\tilde{X} \overset{d}{=} X$, which means that $P(F^{-1}(U)\le x)=P(X\le x)$. We have also used that $P(\xi = F(x)) = 0$ to get that $P(\xi < F(x)) = P(\xi \le F(x))$.

\subsection{Generate a sample of a Pois(1) random variable}
The first thing to notice is that $X$ $\sim$ Pois(1) is a discrete random variable taking values in $\mathbb{Z}=\{0,1,2,\ldots\}$ with density function $f(k) = \frac{\exp(-1)}{k!}$. Since $X$ takes values in $\mathbb{Z}$ and not $\mathbb{Z}_+$ we need to use the above technique not on $X$ itself but on $Y=X+1$. This has density $f_y(k)=\frac{\exp(-1)}{(k-1)!}$ and takes values in $\mathbb{Z}_+$.\\

\noindent
Pseudo code for implementing the technique is presented in algorithm~\vref{Problem2Algo}. In appendix \ref{code2} an implementation in \texttt{R} of a generalization of this algorithm, that generates a vector of independant Pois(1) random variables, is given.\\

\begin{algorithm}
\caption{Generate a $X$ $\sim$ Pois(1) random variable}
\label{Problem2Algo}
\begin{algorithmic}[1]
  \STATE Generate $u$ $\sim$ Unif(0, 1)
  \STATE $\eta \leftarrow 0$
  \STATE $k \leftarrow 1$
  \STATE
  \STATE $\eta_k \leftarrow 1$
  \STATE $\eta \leftarrow \eta + \eta_k$
  \STATE $k \leftarrow k + 1$
  \STATE
  \STATE $f \leftarrow 0$
  \REPEAT
  \STATE $f \leftarrow f + \frac{e^{-1}}{(k-2)!}$
  \IF{$u + 1 - f > 1$}
  \STATE $\eta_k \leftarrow 1$
  \ELSE
  \STATE $\eta_k \leftarrow 0$
  \ENDIF
  \STATE $\eta \leftarrow \eta + \eta_k$
  \UNTIL{$\eta_k = 0$}
  \STATE 
  \RETURN $\eta - 1$
\end{algorithmic}
\end{algorithm}

\noindent
In order to check that the generated sample is indeed a Pois(1) random sample the mean and the variance, which should both theoretically be 1, have been calculated with the following result
\begin{verbatim}
> mean(sample)
[1] 0.994
> var(sample)
[1] 0.9980638
\end{verbatim}

\noindent
We have also made a histogram of the sample and one of a Pois(1) random sample generated by the \texttt{rpois}-function in \texttt{R} in order to compare the two.

\begin{center}
\includegraphics[width=10cm]{problem2_1.eps}	   
\end{center}

\noindent
Furthermore we have calculated a QQ-plot of the sample compared to a Pois(1) random sample generated by the \texttt{rpois}-function in \texttt{R}. Both variables are discrete so in order to get other then a few points a little random noize have been added to both samples.

\begin{center}
\includegraphics[width=10cm]{problem2_2.eps}	   
\end{center}

\newpage